<?php

/**
 * 利用MySQL的LIMIT参数为PHP页面添加分页功能
 * Coding By GF
 */

session_start();
//session_register('pageActionPC');
//session_register('limitStartPC');

// 判断pageActionPC和pageActionMobile变量是否为空，如果为空则赋值"Null"。
empty($_GET['pageActionPC']) ? $_GET['pageActionPC'] = "null" : print "";
empty($_GET['pageActionMobile']) ? $_GET['pageActionMobile'] = "null" : print "";

// 判断limitStartPC和limitStartMobile变量是否为空，如果为空则赋值0。
empty($_SESSION['limitStartPC']) ? $_SESSION['limitStartPC'] = 0 : print "";
empty($_SESSION['pageNumPC']) ? $_SESSION['pageNumPC'] = 1 : print "";
empty($_SESSION['limitStartMobile']) ? $_SESSION['limitStartMobile'] = 0 : print "";
empty($_SESSION['pageNumMobile']) ? $_SESSION['pageNumMobile'] = 1 : print "";

class paging{

    public $perPageRowNum = 28;
    public $maxRowNum = 280 - 28;

    public function conditionPC(){

        if ($_GET['pageActionPC'] == 'first') {
            $_SESSION['limitStartPC'] = 0;
            $_SESSION['pageNumPC'] = 1;
        }elseif ($_GET['pageActionPC'] == 'last' && $_SESSION['limitStartPC'] >= $this->perPageRowNum) {
            $_SESSION['limitStartPC'] -= 28;
            $_SESSION['pageNumPC'] -= 1;
        }elseif ($_GET['pageActionPC'] == 'next' && $_SESSION['limitStartPC'] < $this->maxRowNum) {
            $_SESSION['limitStartPC'] += 28;
            $_SESSION['pageNumPC'] += 1;
        }else {
            echo "";
        }
    }

    public function buttonPC(){

        echo "<p><a style=\"border:#29BB9C solid 1px;padding:0.5em;\" href=./".basename($_SERVER['PHP_SELF'])."?pageActionPC=first>首页<a/>&ensp;";
        echo "<a style=\"border:#29BB9C solid 1px;padding:0.5em;\" href=./".basename($_SERVER['PHP_SELF'])."?pageActionPC=last>上一页<a/>&ensp;";
        echo "第".$_SESSION['pageNumPC']."页 / 共10页&ensp;";
        echo "<a style=\"border:#29BB9C solid 1px;padding:0.5em;\" href=./".basename($_SERVER['PHP_SELF'])."?pageActionPC=next>下一页<a/></p>";

    }

    public function conditionMobile(){

        if ($_GET['pageActionMobile'] == 'first') {
            $_SESSION['limitStartMobile'] = 0;
            $_SESSION['pageNumMobile'] = 1;
        }elseif ($_GET['pageActionMobile'] == 'last' && $_SESSION['limitStartMobile'] >= $this->perPageRowNum) {
            $_SESSION['limitStartMobile'] -= 28;
            $_SESSION['pageNumMobile'] -= 1;
        }elseif ($_GET['pageActionMobile'] == 'next' && $_SESSION['limitStartMobile'] < $this->maxRowNum) {
            $_SESSION['limitStartMobile'] += 28;
            $_SESSION['pageNumMobile'] += 1;
        }else {
            echo "";
        }
    }

    public function buttonMobile(){

        echo "<p><a style=\"border:#29BB9C solid 1px;padding:0.5em;\" href=./".basename($_SERVER['PHP_SELF'])."?pageActionMobile=first>首页<a/>&ensp;";
        echo "<a style=\"border:#29BB9C solid 1px;padding:0.5em;\" href=./".basename($_SERVER['PHP_SELF'])."?pageActionMobile=last>上一页<a/>&ensp;";
        echo "第".$_SESSION['pageNumMobile']."页 / 共10页&ensp;";
        echo "<a style=\"border:#29BB9C solid 1px;padding:0.5em;\" href=./".basename($_SERVER['PHP_SELF'])."?pageActionMobile=next>下一页<a/></p>";
    }

    public function testPC(){

        echo "<br />PC版当前页面动作：".$_GET['pageActionPC'];
        echo "<br />PC版当前从第".$_SESSION['limitStartPC']."开始SELECT数据";
        echo "<br />PC版当前SQL语句：SELECT * FROM table WHERE LIKE % LIMIT ".$_SESSION['limitStartPC'].",".$this->perPageRowNum;
        echo "<br />从第".$_SESSION['limitStartPC']."行开始，显示".$this->perPageRowNum."行";

    }

    public function testMobile(){
        echo "<br />Mobile版当前页面动作：".$_GET['pageActionMobile'];
        echo "<br />Mobile版当前从第".$_SESSION['limitStartMobile']."开始SELECT数据";
        echo "<br />Mobile版当前SQL语句：SELECT * FROM table WHERE LIKE % LIMIT ".$_SESSION['limitStartMobile'].",".$this->perPageRowNum;
        echo "<br />从第".$_SESSION['limitStartMobile']."行开始，显示".$this->perPageRowNum."行";
    }

}

echo '<p type="button" style="color:red;">PHP分页功能(PC版)测试页面</p>';

$classTest = new paging();
$classTest->conditionPC();
$classTest->buttonPC();
$classTest->testPC();

echo '<p type="button" style="color:blueviolet;">PHP分页功能(Mobile版)测试页面</p>';

$classTest = new paging();
$classTest->conditionMobile();
$classTest->buttonMobile();
$classTest->testMobile();

?>
